package com.jrx.anytxn.common.uitl;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @program: anytxn-cf-common
 * @description: 网络相关工具类
 * @author: xiaoyu.jing
 * @date: 2019-11-26 20:31
 **/
public class InternetUtils {

    public static String getLocalIp() throws SocketException {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                InetAddress next = address.nextElement();
                if (next instanceof Inet4Address) {
                    return next.getHostAddress();
                }
            }
        }
        return null;
    }
}
